package com.tr.rentcar.model.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.tr.rentcar.model.GlobalParamHandler;
import com.tr.rentcar.model.bean.Place;
import com.tr.rentcar.model.bean.PlaceStore;
import com.tr.rentcar.model.jdbc.MySqlDb;
import com.tr.rentcar.model.jdbc.ResultHandler;

public class PlaceDao implements ResultHandler<Place> {
	MySqlDb db=MySqlDb.getInstance();

	@Override
	public Place doHander(Map<String, Object> row) {
		Place p=new Place();
		p.setPid(Integer.parseInt(row.get("pid")+""));
		p.setPname(row.get("pname")+"");
		return p;
	}
	/**
	 * 获取所有门店所在地
	 * @return
	 * @throws SQLException
	 */
	public List<Place> getAll() throws SQLException{
		String sql="select * from place";
		return db.executeQuery(sql, this);
	}
	/**
	 * 根据门店所在地ID获取门店
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public List<PlaceStore> getAllByid(String id) throws SQLException{
		String sql="select * from place_store where pid=?";
		return db.executeQuery(sql, new GlobalParamHandler(id), new ResultHandler<PlaceStore>() {

			@Override
			public PlaceStore doHander(Map<String, Object> row) {
				PlaceStore ps=new PlaceStore();
				ps.setPid(Integer.parseInt(row.get("pid")+""));
				ps.setPsId(Integer.parseInt(row.get("ps_id")+""));
				ps.setPsName(row.get("ps_name")+"");
				return ps;
			}
		});
	}
	/**
	 * 通过所在地ID获取地址
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public Place getPlaceById(String id) throws SQLException{
		String sql="select * from place where pid=?";
		return db.queryUninResult(sql, new GlobalParamHandler(id), this);
	}
	
	/**
	 * 通过门店ID获取门店
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public PlaceStore getPlaceStoreById(String id) throws SQLException{
		String sql="select * from place_store where ps_id=?";
		return db.queryUninResult(sql, new GlobalParamHandler(id), new ResultHandler<PlaceStore>() {

			@Override
			public PlaceStore doHander(Map<String, Object> row) {
				PlaceStore ps=new PlaceStore();
				ps.setPid(Integer.parseInt(row.get("pid")+""));
				ps.setPsId(Integer.parseInt(row.get("ps_id")+""));
				ps.setPsName(row.get("ps_name")+"");
				return ps;
			}
		});
	}

}
